package com.beijiteshop.shop.ui.order.adapter

import android.text.SpannableString
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import androidx.core.content.ContextCompat
import com.beijiteshop.libcommon.ext.databinding.inflateBinding
import com.beijiteshop.shop.R
import com.beijiteshop.shop.databinding.IncludeOrderGoodsBinding
import com.beijiteshop.shop.databinding.ItemOrderDetailBinding
import com.beijiteshop.shop.model.api.response.OrderListBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import java.lang.StringBuilder

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/11 14:51
 * @Description
 */
class OrderListAdapter(data: MutableList<OrderListBean>) : BaseQuickAdapter<OrderListBean,BaseDataBindingHolder<ItemOrderDetailBinding>>(
    R.layout.item_order_detail,data),LoadMoreModule {

    init {
        addChildClickViewIds(R.id.order_list_pay_btn)
    }

    override fun convert(
        holder: BaseDataBindingHolder<ItemOrderDetailBinding>,
        item: OrderListBean
    ) {

         holder.dataBinding?.let { outter ->
            outter.order = item
             outter.orderListGoodsContainerLl.removeAllViews()
            item.goods?.forEach {
                val innerBinding = inflateBinding<IncludeOrderGoodsBinding>(LayoutInflater.from(context))
                innerBinding.goods = it
                    outter.orderListGoodsContainerLl.addView(innerBinding.root)

            }

             //订单备注
             val builder = StringBuilder()
             val remarkFormat = context.resources.getString(R.string.order_remark)
             builder.append(remarkFormat)
             builder.append(" ")
             if (!item.remark.isNullOrEmpty()){
                 builder.append(item.remark)
             }
             val spannableStr = SpannableString(builder)
             spannableStr.setSpan(AbsoluteSizeSpan(16,true),remarkFormat.length+1,builder.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
             spannableStr.setSpan(ForegroundColorSpan(ContextCompat.getColor(context,R.color.text_dark)),remarkFormat.length+1,builder.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
             outter.orderListRemarkTv.text = spannableStr

             //付款按钮
             outter.orderListPayBtn.visibility = if (item.isTobePaid ) View.VISIBLE else View.GONE
        }
    }

}